import { routerRedux } from "dva/router";
import { parse } from "qs";
import config from "config";
import { EnumRoleType } from "enums";
import { query, logout } from "services/app";
import * as menuService from "services/menu";
import { infoDetais } from "services/search";
import { userLis, searchs } from "services/search";
import { applicationSave1, applicationUpdate1,getGzcFileList1 } from "services/fileComponent";
import { getarchivesHistory1,getHistoryDetails1 } from "services/fileChange";

const { prefix } = config;
const permissions = JSON.parse(sessionStorage.getItem("permissions"))

export default {
  namespace: "fileChange",
  state: {
    list: "",
    isNew: "",
    visible: false,
    menu: permissions.indexOf("file:fileChange:apply") != -1 ? 0:1,  //判断权限如果是有申请权限则key为0，无则key为1
    details: "",
    userList: "",
    params: "",
    goList:false,
    goHistory:false,
    goDetails:false,
    historyDetails:"",
    infoDetais1:"",
    title:"",
    pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const { query, pathname } = location;
        if (pathname === "/file/fileChange") {
          dispatch({
            type: "useLists",
            payload: location.query
          });
          
        }
      });
    }
  },
  effects: {
    *applicationSave({ payload }, { call, put }) {
      const data = yield call(applicationSave1, payload);
      yield put({ type: "showLoading" });
      if (data.success) {
        yield put({ type: "reload" });
      } else {
        yield put({ type: "hideLoading" });
      }
    },
    *applicationUpdate({ payload }, { call, put }) {
      const data = yield call(applicationUpdate1, payload);
      yield put({ type: "showLoading" });
      if (data.success) {
        yield put({ type: "reload" });
        yield put({
          type: "showDetailSuccess",
          payload: {
            showDetaisl: false
          }
        });
      } else {
        yield put({ type: "hideLoading" });
      }
    },
    *reload(action, { put, select }) {
      const page = yield select(({ fileChange }) => fileChange.pagination);
      const menu = yield select(({ fileChange }) => fileChange.menu)
      yield put({
        type: "searchList",
        payload: {
          page: page.current,
          operatingType: 1,
          operatingState: menu
        }
      });
    },
    // *reloads(action, { put, select }) {
    //   const page = yield select(({ fileChange }) => fileChange.pagination1);
    //   yield put({
    //     type: "searchList",
    //     payload: {
    //       page: page.current,
    //       operatingType: 1,
    //       operatingState: 1
    //     }
    //   });
    // },
    *useLists({ payload }, { call, put }) {
      const data = yield call(userLis, payload);
      if (data.success) {
        yield put({
          type: "querySuccess3",
          payload: {
            userList: data.userList
          }
        });
      }
    },
    *showDet({ payload }, { call, put }) {
      const data = yield call(infoDetais, payload);
      yield put({
        type: "showDetailSuccess",
        payload: {
          details: data
        }
      });
    },
    *searchList({ payload }, { call, put }) {
      const data = yield call(getGzcFileList1, payload);
      //0为申请 1为审批
      if (data.success) {
        yield put({
          type: "querySuccess2",
          payload: {
            params: payload,
            list: data.page.list,
            pagination: {
              total: data.page.totalCount,
              current: data.page.currPage
            }
          }
        });
      }
    },
    *onModalOk({ payload }, { call, put }) {
      yield put({ type: "showLoading" });
      yield put({
        type: "querySuccess",
        payload: {
          visible: false
        }
      });
    },
    *showModal({ payload }, { call, put }) {
      yield put({
        type: "showModalSuccess",
        payload: {
          visible: true,
          isNew: payload.name
        }
      });
    },
    *onCancels({ payload }, { call, put }) {
      yield put({
        type: "showModalSuccess",
        payload: {
          visible: false
        }
      });
    },
    *showMenu({ payload }, { call, put }) {
      yield put({
        type: "querySuccess1",
        payload: {
          menu: payload
        }
      });
    },
    *getHistory({payload},{call,put}){
      const data = yield call (getarchivesHistory1,payload)
      if(data){
        yield put({type:"goHistory1"})
        yield put({
          type:"queryHistorySuccess",
          payload:{
            historyDetails:data.changeList
          }
        })
      }
    },
    *getDetails({payload},{call,put}){
      const data = yield call (getHistoryDetails1,payload)
      if(data){
        yield put({type:'goDetails1'})
        yield put({
          type:"queryDetailsSuccess",
          payload:{
            infoDetais1:data
          }
        })
      } 
    }
  },
  reducers: {
    showDetailSuccess(state, action) {
      const { details } = action.payload;
      return {
        ...state,
        details
      };
    },
    showModalSuccess(state, action) {
      const { visible, isNew } = action.payload;
      return {
        ...state,
        isNew,
        visible
      };
    },
    querySuccess(state, action) {
      const { visible } = action.payload;
      return {
        ...state,
        visible
      };
    },
    querySuccess1(state, action) {
      const { menu } = action.payload;
      return {
        ...state,
        menu
      };
    },
    querySuccess2(state, action) {
      const { list, pagination, params } = action.payload;
      return {
        ...state,
        list,
        params,
        pagination: {
          ...state.pagination,
          ...pagination
        }
      };
    },
    querySuccess3(state, action) {
      const { userList } = action.payload;
      return {
        ...state,
        userList
      };
    },
    queryHistorySuccess(state, action) {
      const { historyDetails } = action.payload;
      return {
        ...state,
        historyDetails
      }
    },
    queryDetailsSuccess(state,action){
      const { infoDetais1 } = action.payload;
      return {
        ...state,
        infoDetais1
      }
    },
    getName(state,action){
      const { title } = action.payload
      return {
        ...state,
        title:title
      }
    },
    showLoading(state) {
      return {
        ...state,
        loading: true
      };
    },
    goList1(state){
      return {
        ...state,
        goList:true
      };
    },
    returnback(state){
      return {
        ...state,
        goList:false
      };
    },
    goHistory1(state){
      return {
        ...state,
        goHistory:true
      };
    },
    returnList(state){
      return{
        ...state,
        goHistory:false
      };
    },
    goDetails1(state){
      return {
        ...state,
        goDetails:true
      };
    },
    returnHistory(state){
      return {
        ...state,
        goDetails:false
      };
    },
  }
};
